package com.inibb.app.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.inibb.app.converter.ShoppingCartConvertor;
import com.inibb.app.dao.JpaGenericDao;
import com.inibb.app.dao.api.IDao;
import com.inibb.app.po.ShoppingCart;
import com.inibb.app.service.IShoppingCartService;
import com.inibb.app.service.security.IUserService;
import com.inibb.app.util.DateUtils;
import com.inibb.app.util.SecurityUtils;
import com.inibb.app.vo.ShoppingCartVO;
import com.inibb.app.vo.security.UserVO;
@Transactional
@Service
public class ShoppingCartServiceImpl implements IShoppingCartService {
	
	private IDao<ShoppingCart, Integer> genericDao;
	
	@Autowired
	protected IUserService userService;
	@PersistenceContext(unitName = "entityManager")
	private EntityManager entityManager ;
		
	@Autowired
	public void setEntityManagerFactory() {
		genericDao = new JpaGenericDao<ShoppingCart, Integer>(entityManager, ShoppingCart.class);
	}

	@Override
	public void delete(Integer cartId) {
			genericDao.delete(cartId);
	}

	@Override
	public List<ShoppingCartVO> getShoppingCart(Integer userId) {
		
		Map<String, Object> criteria = new HashMap<String, Object>();
		criteria.put("userId", userId);
		List<Object[]> obj = genericDao.queryNativeSqlForList("admin", "selectShoppingCartByUserId", criteria);
		
		List<ShoppingCartVO> goodss = new ArrayList<ShoppingCartVO>();
		
			for(Object[] l:obj){
				ShoppingCartVO g = new ShoppingCartVO();
				if(l[0]!=null)
					g.setCartId(Integer.parseInt(l[0].toString()));
				if(l[1]!=null)
					g.setUserId(Integer.parseInt(l[1].toString()));
				if(l[2]!=null)
					g.setGoodsId(Integer.parseInt(l[2].toString()));
				if(l[3]!=null)
					g.setQty(Integer.parseInt(l[3].toString()));
//				if(l[4]!=null)
//					g.setCreateDate(new Date(l[4].toString()));DateUtils.
//				if(l[5]!=null)
//					g.setUpdatedDate(l[5].toString());
				if(l[6]!=null)
					g.setGoodsName(l[6].toString());
				if(l[7]!=null)
					g.setGoodsName2(l[7].toString());
				if(l[8]!=null)
					g.setPrice(l[8].toString());
				if(l[9]!=null)
					g.setInstore(l[9].toString());
				
				goodss.add(g);
			}
		return goodss;
	}

	@Override
	public void saveOrUpdate(ShoppingCartVO vo) {
		UserVO currentUser = userService.getUserByLoginName(SecurityUtils.getUsername());
		Integer userId = currentUser.getId();
		vo.setUserId(userId);
		if(vo.getCartId()==null){
			vo.setCreatedDate(DateUtils.getSystemDate());
			genericDao.merge(ShoppingCartConvertor.converVO2PO(vo));
		}else{
			vo.setUpdatedDate(DateUtils.getSystemDate());
			genericDao.persist(ShoppingCartConvertor.converVO2PO(vo));
		}
	}

}
